package com.hjqjl.module_home.ui;

import android.content.Context;
import android.content.Intent;

import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.hjqjl.common_lib.net.RxTransformerUtils;
import com.hjqjl.module_home.databinding.HomeActivityNetworkBinding;
import com.hjqjl.whapplib.base.BaseActivity;
import com.hjqjl.whapplib.model.GetJoke;
import com.hjqjl.whapplib.net.HttpMethods;
import com.hjqjl.whapplib.net.HttpMethodsNoSingleton;
import com.mp5a5.www.library.use.BaseDisposableObserver;
import com.mp5a5.www.library.use.BaseObserver;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

public class NetWorkActivity extends BaseActivity<HomeActivityNetworkBinding> {
    private CompositeDisposable mCompositeDisposable = null;

    public static void start(Context context) {
        Intent starter = new Intent(context, NetWorkActivity.class);
        context.startActivity(starter);
    }

    @Override
    public void initData() {
        setTitle("联网测试界面");
        mCompositeDisposable = new CompositeDisposable();
        pVBinding.homeTvNetwork.setText("网络是否连接:" + NetworkUtils.isConnected());
    }

    @Override
    public void initListener() {
        pVBinding.homeBtnNetwork1.setOnClickListener(v -> {
            HttpMethods.getInstance().getJoke("1")
                    .compose(RxTransformerUtils.applyTransformer())
                    .compose(this.bindToLifecycle())
                    .subscribe(new BaseObserver<GetJoke>() {

                        @Override
                        public void onSuccess(GetJoke response) {
                            ToastUtils.showShort(response.getMsg());
                            pVBinding.homeTvNetwork.setText("1==>" + new Gson().toJson(response));
                        }
                    });
        });
        pVBinding.homeBtnNetwork2.setOnClickListener(v -> {
            BaseDisposableObserver<GetJoke> disposable = HttpMethods.getInstance()
                    .getJoke("1")
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(new BaseDisposableObserver<GetJoke>(this, true) {
                        @Override
                        public void onSuccess(GetJoke response) {
                            ToastUtils.showShort(response.getMsg());
                            pVBinding.homeTvNetwork.setText("2==>" + new Gson().toJson(response));
                        }
                    });

           /* BaseDisposableSubscriber<NBAKTEntity> disposable = NbaService.getInstance()
                    .getJFNBAInfo("6949e822e6844ae6453fca0cf83379d3")
                    .compose(RxTransformerUtils.flowableTransformer())
                    .subscribeWith(new BaseDisposableSubscriber<NBAKTEntity>(this, true) {
                        @Override
                        public void onSuccess(NBAKTEntity response) {
                            Toast.makeText(MainActivity.this, response.getResult().getTitle(), Toast.LENGTH_SHORT).show();
                        }
                    });*/

            mCompositeDisposable.add(disposable);
        });
        pVBinding.homeBtnNetwork3.setOnClickListener(v -> {
            new HttpMethodsNoSingleton().getJoke("1")
                    .compose(RxTransformerUtils.applyTransformer())
                    .compose(this.bindToLifecycle())
                    .subscribe(new BaseObserver<GetJoke>() {

                        @Override
                        public void onSuccess(GetJoke response) {
                            ToastUtils.showShort(response.getMsg());
                            pVBinding.homeTvNetwork.setText("3==>" + new Gson().toJson(response));
                        }
                    });
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mCompositeDisposable != null && mCompositeDisposable.isDisposed()) {
            mCompositeDisposable.clear();
        }
    }
}
